package com.mnxc.gxa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mnxc.gxa.dto.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mnxc.gxa.entity.Dept;
import com.mnxc.gxa.entity.User;
import com.mnxc.gxa.mapper.DeptMapper;
import com.mnxc.gxa.mapper.UserMapper;
import com.mnxc.gxa.service.IDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.annotation.Resource;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mnxc
 * @since 2023-06-29
 */
@Slf4j
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements IDeptService {
    @Resource
    DeptMapper deptMapper;
    @Resource
    UserMapper userMapper;
    @Override
    public Result getTree() {
        List<Dept> deptList = deptMapper.getAll();
        List<DeptTreeDTO> deptTree = new ArrayList<>();
        deptList.stream().forEach(item->{
            DeptTreeDTO option = new DeptTreeDTO();
            option.setTitle(item.getTitle());
            option.setId(item.getId());
            option.setChildren(new ArrayList<>());

            if(0==item.getPid()){
                deptTree.add(option);
            }
            else {
                deptTree.stream().forEach(opt->{
                    if(opt.getId()== item.getPid()){
                        opt.getChildren().add(option);
                        opt.setLeaf(false);
                    }
                });
            }
        });
        return Result.ok(deptTree);
    }

    @Override
    public Result getAll() {
        // 1.获取数据
        List<Dept> depts = deptMapper.getAll();
        // 2.返回
        return Result.ok(depts, (long) depts.size());
    }

    @Override
    public Result getByCondition(Integer page, Integer pageSize, QueryDeptDTO query) {
        //1.构造分页器
        IPage<Dept> pageInfo = new Page<>(page,pageSize);
        //2.条件查询
        LambdaQueryWrapper<Dept> lqw = new LambdaQueryWrapper<>();
        lqw.like(query.getDeptTitle()!=null,Dept::getTitle,query.getDeptTitle())
                .eq(query.getStatus()!=null,Dept::getStatus,query.getStatus());
        //查询基本信息
        this.page(pageInfo,lqw);
        //获取Plus信息,连表user
        IPage<DeptDTO> pageInfoDTO = new Page<>();
        //拷贝分页信息，除数据体
        BeanUtil.copyProperties(pageInfo,pageInfoDTO,"records");

        List<Dept> records = pageInfo.getRecords();
        List<DeptDTO> dtoList = records.stream().map(item -> {
            DeptDTO deptDTO = BeanUtil.copyProperties(item, DeptDTO.class);
            Long createUserId = item.getCreateBy();
            Long updateUserId = item.getUpdateBy();
            Long pid = item.getPid();
            User user1 = userMapper.selectById(createUserId);
            User user2 = userMapper.selectById(updateUserId);
            Dept parentDept = deptMapper.selectById(pid);
            if(user1!=null){
                deptDTO.setCreateName(user1.getUsername());
            }
            if(user2!=null){
                deptDTO.setUpdateName(user2.getUsername());
            }
            if(parentDept != null){
                deptDTO.setParentName(parentDept.getTitle());
            }else deptDTO.setParentName("无");
            return deptDTO;
        }).collect(Collectors.toList());
        pageInfoDTO.setRecords(dtoList);
        return Result.ok(pageInfoDTO);
    }


    @Override
    public Result updateDept(Dept dept) {
        boolean isSuccess = updateById(dept);
        if (!isSuccess) {
            return Result.fail("更新部门信息失败！");
        }
        return Result.ok("更新部门信息成功！");
    }

    @Override
    public Result deleteDept(Dept dept) {
        boolean isSuccess = deptMapper.deleteDept(dept);
        if (!isSuccess) {
            Result.fail("删除用户失败！");
        }
        return Result.ok("删除用户成功！");
    }

    @Override
    public Result saveDept(Dept dept) {
        boolean isSuccess = save(dept);
        if (!isSuccess) {
            Result.fail("增加用户失败！");
        }
        return Result.ok("增加用户成功！");
    }

    @Override
    public Result freezeDept(Dept dept) {
        boolean isSuccess = deptMapper.freezeDept(dept);
        if (!isSuccess) {
            Result.fail("冻结用户失败！");
        }
        return Result.ok("冻结用户成功！");
    }

    @Override
    public Result active(Dept dept) {
        boolean isSuccess = deptMapper.activeDept(dept);
        if (!isSuccess) {
            Result.fail("激活用户失败！");
        }
        return Result.ok("激活用户成功！");
    }

    @Override
    public Result operateByIds(List<Long> ids) {
        List<Dept> depts = deptMapper.selectBatchIds(ids);
        if(depts.size() != ids.size()){return Result.fail("部门ID不正确"); }
        deptMapper.operateByIds(ids);
        return Result.ok();
    }
    @Override
    public Result operate_freezeByIds(List<Long> ids) {
        List<Dept> depts = deptMapper.selectBatchIds(ids);
        if(depts.size() != ids.size()){return Result.fail("部门ID不正确"); }
        deptMapper.operate_freezeByIds(ids);
        return Result.ok("操作成功！");
    }
    @Override
    public Result operate_deleteByIds(List<Long> ids) {
        List<Dept> depts = deptMapper.selectBatchIds(ids);
        if(depts.size() != ids.size()){return Result.fail("部门ID不正确"); }
        deptMapper.operate_deleteByIds(ids);
        return Result.ok("操作成功！");
    }

}
